package net.oschina.app.ui.activity;

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

import net.oschina.app.R;
import net.oschina.app.base.BaseActivity;
import net.oschina.app.base.BaseFragment;
import net.oschina.app.fragment.explore.ExploreFragment;
import net.oschina.app.fragment.general.GeneralFragment;
import net.oschina.app.fragment.me.MeFragment;
import net.oschina.app.fragment.move.MoveFragment;
import net.oschina.app.ui.MainTab;
import net.oschina.app.view.MyDiaLog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class MainActivity extends BaseActivity/*, RadioGroup.OnCheckedChangeListener */ {
    private static final int IS_EXIT = 99;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.drawerLayout)
    DrawerLayout mDrawerLayout;
    @BindView(R.id.iv_search)
    ImageView mIvSearch;
    @BindView(R.id.fl_content)
    FrameLayout mFlContent;
    @BindView(R.id.ll_menu)
    LinearLayout mLlMenu;
    @BindView(R.id.fth)
    FragmentTabHost mFth;
    @BindView(R.id.iv_add)
    ImageView mIvAdd;

    private List<BaseFragment> mList = new ArrayList<>();

    private ActionBarDrawerToggle mToggle;
    private FragmentManager mFm;
    private boolean isExit = false;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == IS_EXIT) {
                isExit = false;
            }
        }
    };

    @Override
    public int getLayoutRes() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        setToolBar();
    }

    @Override
    public void initData() {
        mList.add(new GeneralFragment());
        mList.add(new MoveFragment());
        mList.add(new ExploreFragment());
        mList.add(new MeFragment());

        mFm = getSupportFragmentManager();
        initFragmentTabHost();
    }

    private void initFragmentTabHost() {
        if (mList.size() > 0) {
            mFth.setup(this, mFm, R.id.fl_content);
            if (Build.VERSION.SDK_INT > 10) { //大于10版本，不显示分割线
                //标签小部件，是一个View，
                TabWidget tabWidget = mFth.getTabWidget();
                //不显示分割线
                tabWidget.setShowDividers(0);
            }

            MainTab[] values = MainTab.values();
            for (int i = 0; i < values.length; i++) {
                MainTab mainTabs = values[i];
                TabHost.TabSpec tabSpec = mFth.newTabSpec(this.getResources().getString(mainTabs.getTag()));
                View view = LayoutInflater.from(this).inflate(R.layout.tab_indicator, null);
                if (i == 2) {
                    view.setVisibility(View.INVISIBLE);
                }
                TextView tv = (TextView) view.findViewById(R.id.tab_title);
                tv.setText(this.getResources().getString(mainTabs.getTag()));
                Drawable drawable = this.getResources().getDrawable(mainTabs.getResId());
                tv.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
                tabSpec.setIndicator(view);
                mFth.addTab(tabSpec, mainTabs.getClazz(), null);
            }
        }
    }

//    private void initFragment() {
//        if (mList.size()>0){
//            mFm.beginTransaction().replace(R.id.fl_content,mList.get(0),"general").commit();
//        }
//    }


    @Override
    public void onBackPressed() {
        if (isExit) {
            super.onBackPressed();
        } else {
            Toast.makeText(this, "再按一次退出程序!", Toast.LENGTH_SHORT).show();
            isExit = true;
            mHandler.sendEmptyMessageDelayed(IS_EXIT, 2000);
        }

    }

    @Override
    public void initListener() {
        // DrawerLayout 和 ToolBar进行关联
        mDrawerLayout.addDrawerListener(drawerListener);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
                    mDrawerLayout.closeDrawer(GravityCompat.START);
                } else {
                    mDrawerLayout.openDrawer(GravityCompat.START);
                }
            }
        });

        mIvAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyDiaLog diaLog = new MyDiaLog(MainActivity.this);
                diaLog.show();
            }
        });

//        mRg.setOnCheckedChangeListener(this);
    }

    private void setToolBar() {
        mToolbar.setNavigationIcon(R.drawable.ic_menu_back);
        mToolbar.setClickable(true);
        // Activity和ToolBar关联一起

        setSupportActionBar(mToolbar);
        mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
        //该方法会自动和actionBar关联
        mToggle.syncState();
    }

    DrawerLayout.DrawerListener drawerListener = new DrawerLayout.DrawerListener() {
        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            mToggle.onDrawerSlide(drawerView, slideOffset);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            mToggle.onDrawerOpened(drawerView);
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            mToggle.onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerStateChanged(int newState) {
            mToggle.onDrawerStateChanged(newState);
        }
    };

//    @Override
//    public void onCheckedChanged(RadioGroup group, int checkedId) {
//        switch (checkedId){
//            case R.id.rb_general:
//                mFm.beginTransaction().replace(R.id.fl_content,mList.get(0),"general").commit();
//                break;
//            case R.id.rb_move:
//                mFm.beginTransaction().replace(R.id.fl_content,mList.get(1),"move").commit();
//                break;
//            case R.id.rb_explore:
//                mFm.beginTransaction().replace(R.id.fl_content,mList.get(2),"explore").commit();
//                break;
//            case R.id.rb_me:
//                mFm.beginTransaction().replace(R.id.fl_content,mList.get(3),"me").commit();
//                break;
//        }
//    }
}
